﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace ExtendPropertyLib.WPF
{
   public class RelayCommand : ICommand 
 {
     private readonly Action _execute;

     private readonly Action<object> _executeO;

     private readonly Func<bool> _canExecute;

     public RelayCommand(Action execute)
         : this(execute, null)
     {
     }

     public RelayCommand(Action execute, Func<bool> canExecute)
     {
         if (execute == null)
         {
             throw new ArgumentNullException("execute");
         }
 
         _execute = execute;
         _canExecute = canExecute;
     }

     public RelayCommand(Action<object> execute, Func<bool> canExecute)
     {
         if (execute == null)
         {
             throw new ArgumentNullException("execute");
         }

         _executeO = execute;
         _canExecute = canExecute;
     }



     public event EventHandler CanExecuteChanged;
 
     public void RaiseCanExecuteChanged()
     {
         var handler = CanExecuteChanged;
         if (handler != null)
         {
             handler(this, EventArgs.Empty);
         }
     }
 
     public bool CanExecute(object parameter)
     {
         return _canExecute == null ? true : _canExecute();
     }
 
     public void Execute(object parameter)
     {
         if (_executeO != null)
             _executeO(parameter);
         else
            _execute();
     }
 }
}
